package com.gxa.system_setting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.common.R;
import com.gxa.common.entity.system_settings.BadWeather;
import com.gxa.system_setting.service.BadWeatherService;
import com.gxa.system_setting.mapper.BadWeatherMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class BadWeatherServiceImpl implements BadWeatherService {

    @Autowired
    private BadWeatherMapper badWeatherMapper;

    @Override
    @Cacheable(cacheNames = "BadWeatherList",key = "#currentPage+'-'+#pageSize")
  //  @CacheEvict(value = "BadWeather",allEntries=true) 每次都会清除缓存
    public R queryBadWeatherList(Integer currentPage, Integer pageSize) {
        R r=new R();
        QueryWrapper<BadWeather> queryWrapper = new QueryWrapper<>();
        Page page=new Page(currentPage,pageSize);
        Page BadWeatherList = badWeatherMapper.selectPage(page, null);
        r.setCode(200);
        r.setMessage("查询成功");
        r.setData(BadWeatherList);
        return r;
    }


    @Override
    public R updateBadWeather(BadWeather badWeather) {
        R r=new R();
        QueryWrapper<BadWeather> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0)
                    .eq("badweather_id",badWeather.getBadweatherId());
        int i = badWeatherMapper.update(badWeather, queryWrapper);
        r.setCode(200);
        r.setMessage("修改成功");
        r.setData(i);
        return r;
    }

    @Override
    public R stopBadWeather(BadWeather badWeather) {
        R r=new R();
        QueryWrapper<BadWeather> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",1)
                    .eq("badweather_id",badWeather.getBadweatherId());
        badWeather.setStatus(3);//将status变为3
        int i = badWeatherMapper.update(badWeather, queryWrapper);
        r.setCode(200);
        r.setMessage("修改成功");
        r.setData(i);
        return r;
    }
    //(A or B )and C查询
    @Override
    public R againPut(BadWeather badWeather) {
        R r=new R();
        QueryWrapper<BadWeather> queryWrapper = new QueryWrapper<>();
        queryWrapper .and((wrapper) -> {
            wrapper.eq("status", 2).or().eq("status", 3);
        });
        queryWrapper .eq("badweather_id",badWeather.getBadweatherId());
        badWeather.setStatus(1);//将2或3变为1
        int i = badWeatherMapper.update(badWeather, queryWrapper);
        r.setCode(200);
        r.setMessage("重新发布成功");
        r.setData(i);
        return r;
    }

    @Override
    public R queryByConditions(Map map) {
        R  r=new R();

        List<BadWeather> badWeatherList= badWeatherMapper.queryByConditions(map);
        r.setCode(200);
        r.setMessage("条件查询成功");
        r.setData(badWeatherList);
        return r;
    }

}
